var TXT_NETBUSY = "\u7f51\u7edc\u7e41\u5fd9\uff0c\u8bf7\u7a0d\u540e\u518d\u8bd5\u2026";
var TXT_NETWAIT = "\u6b63\u5728\u83b7\u53d6\u6570\u636e\uff0c\u8bf7\u7a0d\u5019\u2026";
var TXT_POSTDATA = "\u6b63\u5728\u63d0\u4ea4\u6570\u636e\uff0c\u8bf7\u7a0d\u5019\u2026";
var _SEARCH_CANCEL = false;
var _SEARCH_TYPE = "";
var _SEARCH_COUNT = 0;
var _SEARCH_UNIT = 2;
var _HOSTNAME = location.hostname.split(".");
_HOSTNAME = [ _HOSTNAME.pop(), _HOSTNAME.pop() ].reverse().join(".");
var _PROTOCOL = location.protocol;
var _SITE_PEANUT = "www." + _HOSTNAME;
var _SITE_DOMAIN = "domain." + _HOSTNAME;
var _SITE_ASK = "ask." + _HOSTNAME;
var _SITE_STATIC = "www.im315.com";
var _ISLOGIN = isLogin();
String.prototype.toInt = function() {
	return $.func.checkInt(this)
};
function isLogin() {
	return !!($.cookie("auth") && $.cookie("account"))
}
function moveOption(c, b) {
	c = $(c)[0];
	b = $(b)[0];
	for ( var a = c.options.length - 1; a >= 0; a--) {
		if (c.options[a].selected && c.options[a].value != "") {
			b.options.add(new Option(c.options[a].text, c.options[a].value));
			c.options[a] = null
		}
	}
}
function checkboxAll(a, b) {
	if (b.type != "checkbox") {
		return false
	}
	if (b.type != "checkbox") {
		b.checked = b.checked === null ? true : !b.checked
	}
	$('input[name="' + a + '"]:checkbox:enabled').attr("checked", b.checked)
}
function checkboxVal(a) {
	var b = [];
	$("input:checkbox[name=" + a + "]:enabled:checked").each( function() {
		b.push(this.value)
	});
	return b
}
function getData(o) {
	return eval("false||" + $(o).attr("data"))
}
function setTag(f) {
	var d = $("#tags").val().split(" ");
	var c = [], b = {};
	var e = !$(f).hasClass("orange");
	var a = $(f).html();
	if (e) {
		d.push(a)
	} else {
		d = $.grep(d, function(g) {
			return g != a
		})
	}
	$.each(d, function() {
		if (!b[this] && this != "") {
			b[this] = true;
			c.push(this)
		}
	});
	$("#tags").val(c.join(" "));
	$(f).toggleClass("orange")
}
function checkDomain(a, d, f) {
	var b = this;
	if (!a.length) {
		return
	}
	if (!$.isFunction(d)) {
		return
	}
	if (a.constructor != Array) {
		a = [ a ]
	}
	var e = [];
	for ( var c = 0; (c < _SEARCH_UNIT && a.length > 0); c++) {
		e.push(a.shift())
	}
	$.ajax( {
		type :"POST",
		url :"/domain/check.php",
		data : {
			domain :e.join(","),
			free :f ? 1 : 0
		},
		dataType :"json",
		success : function(g) {
			d.apply(b, [ g, a.length, f ]);
			if (a.length && !_SEARCH_CANCEL) {
				checkDomain.apply(b, [ a, d, f ])
			}
		},
		error : function(g) {
			d.apply(b, [ e, a.length, f ]);
			if (a.length && !_SEARCH_CANCEL) {
				checkDomain.apply(b, [ a, d, f ])
			}
		}
	})
}
function checkStatus(c) {
	var a = c.className;
	var b = c.name;
	if (a == "disable" || a == "expire") {
		window.open(_PROTOCOL + "//" + _SITE_DOMAIN + "/whois/?domain="
				+ encodeURIComponent(b), "whois")
	} else {
		if (a == "enable") {
			if (location.hostname == _SITE_DOMAIN) {
				addPending(b, true)
			} else {
				window.open("http://" + _SITE_PEANUT
						+ "/payment/domain_reg.php?domain="
						+ encodeURIComponent(b), "domainreg")
			}
		} else {
			if (a == "loading") {
				alert(TXT_POSTDATA)
			} else {
				if (_SEARCH_COUNT < 0) {
					_SEARCH_COUNT = 0
				}
				if (_SEARCH_COUNT >= 5) {
					return alert(TXT_POSTDATA)
				}
				$("a[name=" + b + "]").attr("class", "loading");
				checkDomain(b, updateStatus);
				_SEARCH_COUNT++
			}
		}
	}
}
function updateStatus(c, b) {
	var a = "timeout";
	if (typeof c == "string") {
		$("a[name=" + c + "]").attr("class", a);
		return
	}
	$(c).each( function() {
		var d = this.domain ? this.domain : this;
		if (this.ret == 100) {
			a = "enable"
		} else {
			if (this.ret == 0) {
				a = "disable"
			}
		}
		$("a[name=" + d + "]").attr("class", a);
		_SEARCH_COUNT--
	})
}
function toRegister(b, c) {
	if (!b) {
		b = checkboxVal("domain")
	}
	if (b.constructor != Array) {
		b = [ b ]
	}
	if (!b.length) {
		return alert("\u8bf7\u9009\u62e9\u8981\u6ce8\u518c\u7684\u57df\u540d")
	}
	if (c) {
		var a = _PROTOCOL + "//" + _SITE_PEANUT
				+ "/payment/domain_free.php?domain="
				+ encodeURIComponent(b.join(","))
	} else {
		var a = _PROTOCOL + "//" + _SITE_PEANUT
				+ "/payment/domain_reg.php?domain="
				+ encodeURIComponent(b.join(","))
	}
	window.open(a)
}
function initSearch() {
	var a = $("#dm-sc-result");
	a.find("a.icon-stop").click(cancelSearch);
	a.find(".bottom button[name=reg]").click( function() {
		var b = [];
		$("#dm-result :checked").each( function() {
			b.push(this.value)
		});
		toRegister(b, (_SEARCH_TYPE == "free"))
	});
	a.find(".bottom button[name=retry]").click(searchTimeoutDomain);
	a.find(".bottom button[name=collect]").click( function() {
		$("#prefixform").submit()
	})
}
function searchDomain(h, d, f) {
	var c = (f == "free");
	var a = [];
	var e = false;
	if (!d) {
		d = ""
	}
	if (typeof h == "string") {
		h = [ h ];
		e = true
	}
	if (typeof d == "string") {
		d = [ d ]
	}
	$(h).each( function() {
		var i = this;
		$(d).each( function() {
			var j = this;
			a.push(i + j)
		})
	});
	_SEARCH_CANCEL = false;
	_SEARCH_TYPE = f;
	this.header.find(".loading,a.icon-stop").show();
	this.footer.find("button").hide();
	var g = this.footer.find("[name=collect]");
	$("#dm-prefix").hide();
	$("#dm-hint").toggle(_SEARCH_TYPE == "cn");
	$("#dm-result").html("");
	var b = this;
	if (_ISLOGIN && !c && e) {
		$.ajax( {
			type :"POST",
			url :"/domain/prefix.php",
			data : {
				name :h[0]
			},
			dataType :"html",
			success : function(i) {
				if (!i) {
					$("#dm-prefix").html("").hide()
				} else {
					$("#dm-prefix").html(i).show();
					g.show();
					var j = validatePrefix("#prefixform", function() {
						$("#dm-prefix").html("").hide();
						g.hide()
					})
				}
			},
			error : function(i) {
				$("#dm-prefix").hide()
			}
		})
	}
	checkDomain(a, updateResult, c)
}
function searchTimeoutDomain() {
	var a = [];
	$("#dm-result .icon-timeout").each( function(c) {
		var d = $(this).parent();
		a.push(d.attr("for"));
		d.remove()
	});
	var b = $("#dm-sc-result");
	b.find(".loading,a.icon-stop").show();
	b.find("button").hide();
	checkDomain(a, updateResult, (_SEARCH_TYPE == "free"))
}
function updateResult(k, b, i) {
	if (typeof k == "string") {
		k = k.split(",")
	}
	var a = '<input id="$root$" type="checkbox" value="$root$" onclick="pendingReg(\'$root$\',this)" /><span class="icon icon-enabled"><em>\u672a\u6ce8\u518c</em></span>$root$';
	var l = '<span class="icon icon-disabled"><em>\u5df2\u6ce8\u518c</em></span><a href="http://' + _SITE_DOMAIN + '/whois/?domain=$domain$" target="_blank">$root$</a>';
	var d = '<span class="icon icon-timeout"><em>\u8d85\u65f6</em></span>$root$';
	var h = '<span class="icon icon-disabled"><em>\u5df2\u6ce8\u518c</em></span>$root$';
	var c = '<input id="$root$" type="radio" name="radiobutton" value="$root$" /><span class="icon icon-enabled"><em>\u672a\u6ce8\u518c</em></span>$root$';
	if (i) {
		a = c;
		l = h
	}
	var f = 50, e = "", j = "", g = "";
	$.each(k, function() {
		e = (typeof this.domain == "string") ? this.domain : this;
		f = (typeof this.ret == "number") ? this.ret : 50;
		if (f == 100) {
			j = a
		} else {
			if (f == 0) {
				j = l
			} else {
				f = 50;
				j = d
			}
		}
		j = '<label for="$root$">' + j + "</label> ";
		j = j.replace(/\$root\$/g, e);
		j = j.replace(/\$domain\$/g, encodeURI(e));
		g += j
	});
	$("#dm-result").append(g);
	if (!b) {
		finishSearch()
	}
}
function cancelSearch() {
	_SEARCH_CANCEL = true;
	finishSearch(true)
}
function finishSearch(a) {
	if (_SEARCH_CANCEL & !a) {
		return
	}
	var c = $("#dm-sc-result");
	c.find(".loading,a.icon-stop").hide();
	if ($("#dm-result").find(".icon-enabled").size()) {
		c.find(".bottom button[name=reg]").show()
	}
	if ($("#dm-result").find(".icon-timeout").size()) {
		c.find(".bottom button[name=retry]").show()
	}
}
function pendingReg(a, b) {
	if (!_ISLOGIN) {
		return
	}
	if ($(b).attr("done")) {
		return
	}
	$(b).attr("done", true);
	if (location.hostname == _SITE_DOMAIN) {
		addPending(a, true);
		return
	}
	$.ajax( {
		type :"POST",
		url :"/domain/domain.php",
		data : {
			act :"pending",
			domain :a
		},
		dataType :"json",
		success : function(c) {
		},
		error : function(c) {
		}
	})
}
function savePrefix(b, a) {
	if (!b) {
		return false
	}
	var c = $(b).serializeArray();
	$(b).disabled();
	$.ajax( {
		type :"POST",
		url :$(b).attr("action"),
		data :c,
		dataType :"json",
		success : function(d) {
			alert(d.msg);
			if (d.success) {
				if (a) {
					a()
				} else {
					if (_SITE_DOMAIN == location.hostname) {
						window.location = "../home/"
					}
				}
			} else {
				$(b).enabled()
			}
		},
		error : function(d) {
			alert(TXT_NETBUSY);
			$(b).enabled()
		}
	})
}
function validatePrefix(c, b) {
	var a = $(c)
			.validate(
					{
						onkeyup :false,
						focusInvalid :false,
						onfocusin :false,
						onfocusout :false,
						errorClass :false,
						showErrors : function(f, e) {
							var d = this.errorList[0];
							if (d) {
								d.element.focus();
								alert(d.message);
								return
							}
						},
						submitHandler : function(d) {
							savePrefix(d, b)
						},
						rules : {
							name : {
								required :true
							},
							classid : {
								required :true,
								min :0
							},
							memo : {
								required :true,
								rangelength : [ 3, 200 ]
							},
							tags : {
								required :true
							}
						},
						messages : {
							name :"\u8bf7\u8f93\u5165\u9177\u7c73\u540d\u79f0",
							classid :"\u8bf7\u9009\u62e9\u8d77\u540d\u7c7b\u578b",
							tags : {
								required :"\u8bf7\u8f93\u5165\u9177\u7c73\u6807\u7b7e"
							},
							memo : {
								required :"\u8bf7\u8f93\u5165\u6982\u8981\u8bf4\u660e",
								rangelength :$
										.format("\u6982\u8981\u8bf4\u660e\u957f\u5ea6{0}-{1}\u4e4b\u95f4")
							}
						}
					});
	return a
};
